#include "server.h"

int main(){
    int retnumber;

    int server_socket_fd;
    retnumber = initSocket(&server_socket_fd) ;
    

    printf("开始监听\n");
    retnumber = listen(server_socket_fd,100) ;
    if(retnumber == -1){
        perror("listen error\n");
        exit(1);
    }

    struct sockaddr_in client_addr;
    socklen_t client_addr_size = sizeof(client_addr);

    int client_socket_fd = accept(server_socket_fd,(struct sockaddr*)&client_addr,&client_addr_size) ;
    if(client_socket_fd == -1){
        perror("accept error\n");
        exit(1);
    }
    
    char readbuffer[1024] = {0};
    retnumber = read(client_socket_fd,readbuffer,1024);
    if(retnumber == -1){
        perror("read error\n");
        exit(1);
    }
       
    close(client_socket_fd);
    close(server_socket_fd);
   
    return 0 ;
}

int initSocket(int *sockfd){
    *sockfd = socket(AF_INET,SOCK_STREAM,0) ;
    if(*sockfd == -1){
        perror("socket error\n") ;
        return -1 ;
    }

    struct sockaddr_in server_addr ;
    memset(&server_addr,0,sizeof(server_addr));
    server_addr.sin_family = AF_INET ;
    server_addr.sin_port = htons(SERVER_PORT);
    server_addr.sin_addr.s_addr = htonl(SERVER_IP);

    int retnumber = bind(*sockfd,(struct sockaddr *)&server_addr,sizeof(server_addr)) ;
    if(retnumber == -1){
        perror("bind error\n");
        return -1;
    }

    return 0;
}